<?php
namespace app\api\controller;

use app\common\controller\ApiBase;
use think\facade\Db;

class DataCompany extends ApiBase
{

    public function getCompany()
    {
        $param = $this->request->param();
        if( empty($param['keyword']) ){
            return $this->jsonFmt('参数有误');
        }
        $data = [];
        $keyword = trim($param['keyword']);
        $data = Db::name('data_company')->field('title,regMoney,address,province,city')->where('title',$keyword)->find();
        if(empty($data)){
            return $this->jsonFmt('没有相关数据');
        }
        return $this->jsonFmt('请求成功', $data, 1);
    }

    public function putCompany()
    {
        $param = $this->request->param();
        if( empty($param['keyword']) ){
            return $this->jsonFmt('参数有误');
        }
        $data = [];
        $keyword = trim($param['keyword']);
        $data = Db::name('data_company')->field('id')->where('title',$keyword)->find();
        if(!empty($data)){
            return $this->jsonFmt('公司已存在');
        }
        $com = apiGetCom($keyword);
        $com = json_decode($com,true);
        if(empty($com['result'])){
            return $this->jsonFmt('查询失败');
        }
        $com = $com['result'];
        $com['title'] = $keyword; 
        Db::name('data_company')->insert($com);
        return $this->jsonFmt('执行成功');
    }

}